package com.ithema.notebook.window;

import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.ToolWindow;
import com.ithema.notebook.data.DataCenter;
import com.ithema.notebook.data.JTableUtils;
import com.ithema.notebook.notify.NotifyType;
import com.ithema.notebook.notify.SystemNotify;
import com.ithema.notebook.processor.DefaultSourceNoteData;
import com.ithema.notebook.processor.MDFreemarkerProcessor;
import com.ithema.notebook.processor.Processor;
import com.ithema.notebook.processor.SourceNoteData;
import org.apache.commons.collections.CollectionUtils;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.util.Objects;

/**
 * Package: com.ithema.notebook.window
 * Description：
 *
 * @author: huangjian
 * Date:  2020/10/18 16:27
 * Modified By:
 */
public class NoteListWindow {
    private JTextField tfTopic;
    private JTable tbContent;
    private JButton btnCreate;
    private JButton btnClose;
    private JButton btnClear;
    private JPanel contentPanel;
    private JButton btnRemove;
    private JButton btnTop;
    private JButton btnDown;

    private void init(){
        tbContent.setModel(DataCenter.DEFAULT_TABLE_MODEL);
        tbContent.setRowHeight(40);
        tbContent.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        tbContent.getTableHeader().setReorderingAllowed(false);
    }
    public NoteListWindow(Project project, ToolWindow toolWindow) {
        init();
        btnCreate.addActionListener(e -> {
            String topic = tfTopic.getText();
            String fileName=topic+".md";
            if(null==topic||"".equals(topic.trim())){
                SystemNotify.notify(NotifyType.TOPIC_IS_EMPTY);
                return;
            }

            if(CollectionUtils.isEmpty(DataCenter.NOTE_LIST)){
                SystemNotify.notify(NotifyType.CONTENT_IS_EMPTY);
                return;
            }
            VirtualFile virtualFile = FileChooser.chooseFile(FileChooserDescriptorFactory.createSingleFolderDescriptor(),
                    project,
                    project.getBaseDir());
            if(Objects.nonNull(virtualFile)){
                String path = virtualFile.getPath();
                String fullPath=path.concat("/").concat(fileName);
                Processor processor=new MDFreemarkerProcessor();
                SourceNoteData sourceNoteData=new DefaultSourceNoteData(fullPath,
                        DataCenter.NOTE_LIST, topic);
                try {
                    processor.process(sourceNoteData);
                    SystemNotify.notify(NotifyType.CREATE_DOCUMENT_SUCCESS,fullPath);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
        btnClear.addActionListener(e -> {
            DataCenter.reset();
        });
        btnClose.addActionListener(e -> {
            toolWindow.hide(null);
        });
        //删除选中行
        btnRemove.addActionListener(e -> {
            int row = tbContent.getSelectedRow();
            if(row==-1){
                SystemNotify.notify(NotifyType.REMOVE_CONTENT_SELECT_NULL);
                return;
            }
            //删除所选行;
            DataCenter.DEFAULT_TABLE_MODEL.removeRow(row);
            DataCenter.NOTE_LIST.remove(row);
        });
        //上移动
        btnTop.addActionListener(e -> {
            JTableUtils.moveUpRow(tbContent, (DefaultTableModel) tbContent.getModel(),tbContent.getColumnCount());
        });
        //下移动
        btnDown.addActionListener(e -> {
            JTableUtils.moveDownRow(tbContent, (DefaultTableModel) tbContent.getModel(),tbContent.getColumnCount());
        });
    }

    public JPanel getContentPanel() {
        return contentPanel;
    }
}
